package com.xinfu.newproject.model.matter_repair;

import android.util.Log;

import com.google.gson.Gson;
import com.xinfu.newproject.apiservice.JsNetworkService;
import com.xinfu.newproject.bean.matter_repair.AddRepairBean;
import com.xinfu.newproject.bean.matter_repair.AddRepairBody;
import com.xinfu.newproject.bean.matter_repair.bean.RepairEvaluateBean;
import com.xinfu.newproject.bean.matter_repair.body.RepairEvaluateBody;
import com.xinfu.newproject.callback.MyCallBack;
import com.xinfu.newproject.model.IModel;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class AddRepairModel implements IModel.ISubmitRepairModel {

    @Override
    public void submitRepair(AddRepairBody addRepairBody, MyCallBack<AddRepairBean> callBack) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", addRepairBody.getUserId());
        map.put("contents", addRepairBody.getContents());
        map.put("userName", addRepairBody.getUserName());
        map.put("mobile", addRepairBody.getMobile());
        map.put("attachFile", addRepairBody.getAttachFile());
        map.put("repairPartId", addRepairBody.getRepairPartId());
        map.put("repairWay", addRepairBody.getRepairWay());
        map.put("tenantId", addRepairBody.getTenantId());
        map.put("projectId", addRepairBody.getProjectId());
        map.put("roomId", addRepairBody.getRoomId());
        map.put("repairArea", addRepairBody.getRepairArea());
        if (addRepairBody.getRepairDate() != null) {
            if (!addRepairBody.getRepairDate().equals("")) {
//                map.put("repairDate", addRepairBody.getRepairDate());
                map.put("bespeakFromTime", addRepairBody.getBespeakFromTime());
//                map.put("bespeakToTime", addRepairBody.getBespeakToTime());
            }
        }
        Log.i("testImage", new Gson().toJson(map));
        JsNetworkService.getInstance().getIJsNetworkService()
                .submitRepair(addRepairBody.getHeader(), map)
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<AddRepairBean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        Disposable mDisposable; //用于Activity销毁时停止执行
                        mDisposable = d;
                        callBack.onDispose(mDisposable);
                    }

                    @Override
                    public void onNext(@NonNull AddRepairBean addRepairBean) {

                        callBack.onSuccess(addRepairBean);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        callBack.onError("Server Error", "网络异常，请稍后再试");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


    /**
     * 报修评价
     */

    @Override
    public void repairEvaluate(RepairEvaluateBody repairEvaluateBody, MyCallBack<RepairEvaluateBean> callBack) {
        Map<String, Object> map = new HashMap<>();
        map.put("repairId", repairEvaluateBody.getRepairId());
        map.put("evaluateContent", repairEvaluateBody.getReviewContent());
        map.put("score", repairEvaluateBody.getScore());
        map.put("userName", repairEvaluateBody.getUserName());
        if (repairEvaluateBody.getCommentImg() != null){
            map.put("evaluateImage", repairEvaluateBody.getCommentImg());
        }

        JsNetworkService.getInstance().getIJsNetworkService()
                .repairEvaluate(repairEvaluateBody.getHeader(), map)
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<RepairEvaluateBean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        Disposable mDisposable; //用于Activity销毁时停止执行
                        mDisposable = d;
                        callBack.onDispose(mDisposable);
                    }

                    @Override
                    public void onNext(@NonNull RepairEvaluateBean repairEvaluateBean) {

                        callBack.onSuccess(repairEvaluateBean);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        callBack.onError("Server Error", "网络异常，请稍后再试");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
